package com.zjdiepu.www.biz.cw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.cw.CwRemitAdjustBMapper;
import com.zjdiepu.www.dao.cw.CwRemitAdjustbSubMapper;
import com.zjdiepu.www.dao.fk.FkAuditListMapper;
import com.zjdiepu.www.ibiz.cw.ICwRemitAdjustBService;
import com.zjdiepu.www.model.cw.dto.CwRemitAdjustB;
import com.zjdiepu.www.model.cw.dto.CwRemitAdjustbSub;
import com.zjdiepu.www.model.cw.ext.CwRemitAdjustBExt;
import com.zjdiepu.www.model.fk.dto.FkAuditList;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年08月02日 15时40分<br/>
 * @since 1.0<br/>
 */
@Service
public class CwRemitAdjustBServiceImpl extends BaseService implements ICwRemitAdjustBService {
	public static final Logger logger = LoggerFactory.getLogger(CwRemitAdjustBServiceImpl.class);

	@Autowired
	private CwRemitAdjustBMapper cwRemitAdjustBMapper;
	@Autowired
	private CwRemitAdjustbSubMapper cwRemitAdjustBSubMapper;
	@Autowired
	private FkAuditListMapper fkAuditListMapper;
	
	
	
	public void saveAll(CwRemitAdjustB cwRemitAdjustB,CwRemitAdjustbSub cwRemitAdjustbSub, FkAuditList fkAuditList) {
		cwRemitAdjustBMapper.insert(cwRemitAdjustB);
		//保存垫款调整B子表
		cwRemitAdjustbSub.initInsertData(cwRemitAdjustbSub.getModifier(), cwRemitAdjustbSub.getModifierName());
		cwRemitAdjustBSubMapper.insert(cwRemitAdjustbSub);
		
		fkAuditList.initInsertData(fkAuditList.getModifier(), fkAuditList.getModifierName());
		fkAuditListMapper.insert(fkAuditList);
	}
	
	public void updateAll(CwRemitAdjustB cwRemitAdjustB,CwRemitAdjustbSub cwRemitAdjustbSub) {
//		cwRemitAdjustB.initInsertData(cwRemitAdjustB.getModifier(), cwRemitAdjustB.getModifierName());
		cwRemitAdjustBMapper.updateByPrimaryKeySelective(cwRemitAdjustB);
		//保存垫款调整B子表
//		cwRemitAdjustbSub.initInsertData(cwRemitAdjustbSub.getModifier(), cwRemitAdjustbSub.getModifierName());
		cwRemitAdjustBSubMapper.updateByPrimaryKeySelective(cwRemitAdjustbSub);
	}
	
	@Override
	public void saveUpdateAndDelete(CwRemitAdjustB cwRemitAdjustB, OperateType operType) {
		switch (operType) {
		case add:
			cwRemitAdjustBMapper.insert(cwRemitAdjustB);
			break;
		case edit:
			cwRemitAdjustB.initUpdateData(cwRemitAdjustB.getModifier(), cwRemitAdjustB.getModifierName());
			// 影响的记录数
			int updateCount = cwRemitAdjustBMapper.updateByPrimaryKeySelective(cwRemitAdjustB);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			cwRemitAdjustB.initDeleteData(cwRemitAdjustB.getModifier(), cwRemitAdjustB.getModifierName());
			// 影响的记录数
			int deleteCount = cwRemitAdjustBMapper.updateByPrimaryKeySelective(cwRemitAdjustB);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<CwRemitAdjustB> selectByExt(CwRemitAdjustBExt cwRemitAdjustBExt) {
		return cwRemitAdjustBMapper.selectByExt(cwRemitAdjustBExt);
	}
	
	@Override
	public int countByExt(CwRemitAdjustBExt cwRemitAdjustBExt){
		return cwRemitAdjustBMapper.countByExt(cwRemitAdjustBExt);
	}
	
	@Override
	public CwRemitAdjustB selectById(String id) {
		return cwRemitAdjustBMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			cwRemitAdjustBMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(CwRemitAdjustB cwRemitAdjustB) {
		return cwRemitAdjustBMapper.countRefined(cwRemitAdjustB);
	}

	@Override
	public CwRemitAdjustB selectRefined(CwRemitAdjustB cwRemitAdjustB) {
		return cwRemitAdjustBMapper.selectRefined(cwRemitAdjustB);
	}

	@Override
	public boolean repeat(CwRemitAdjustB cwRemitAdjustB) {
		int count = countRefined(cwRemitAdjustB);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(cwRemitAdjustB.getId())) {
			return true;
		}
		CwRemitAdjustB dbItem = selectRefined(cwRemitAdjustB);
		if(null == dbItem) {
			return false;
		}
		return !cwRemitAdjustB.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return cwRemitAdjustBMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
}
